package cn.vawn.base.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

public final class ParamUtil {
    private static final Logger logger = LoggerFactory.getLogger(PropertyUtil.class);

    public static String getClientIp(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        int index = ip.indexOf(',');
        if (index != -1) {
            return ip.substring(0, index);
        }
        return ip;
    }

    public static String getString(HttpServletRequest request, String param) {
        return request.getParameter(param);
    }

    public static String getString(HttpServletRequest request, String param, String defaultVal) {
        String val = request.getParameter(param);
        return val == null ? defaultVal : val;
    }

    public static String[] getStrings(HttpServletRequest request, String param) {
        return request.getParameterValues(param);
    }

    public static boolean getBoolean(HttpServletRequest request, String param) {
        return StringUtil.getBoolean(getString(request, param));
    }

    public static boolean[] getBooleans(HttpServletRequest request, String param) {
        String[] vals = getStrings(request, param);
        boolean[] rets = null;
        if (vals != null) {
            rets = new boolean[vals.length];
            for (int i = 0; i < vals.length; i++) {
                rets[i] = StringUtil.getBoolean(vals[i]);
            }
        }
        return rets;
    }

    public static int getInt(HttpServletRequest request, String param) {
        String val = getString(request, param);
        int ret = 0;
        if (!StringUtil.isEmpty(val)) {
            try {
                ret = Integer.parseInt(val);
            } catch (Exception ex) {
                logger.warn(String.format("The input [%s] is not a integer.", val));
            }
        }
        return ret;
    }

    public static int[] getInts(HttpServletRequest request, String param) {
        String[] vals = getStrings(request, param);
        int[] rets = null;
        if (vals != null) {
            rets = new int[vals.length];
            for (int i = 0; i < vals.length; i++) {
                if (!StringUtil.isEmpty(vals[i])) {
                    try {
                        rets[i] = Integer.parseInt(vals[i]);
                    } catch (Exception ex) {
                        logger.warn(String.format("The input [%s] is not a integer.", vals[i]));
                        rets[i] = -1;
                    }
                }
            }
        }
        return rets;
    }

    public static int getInt(HttpServletRequest request, String param, int defaultVal) {
        String val = getString(request, param);
        int ret = defaultVal;
        if (!StringUtil.isEmpty(val)) {
            try {
                ret = Integer.parseInt(val);
            } catch (Exception ex) {
                logger.warn(String.format("The input [%s] is not a integer.", val));
            }
        }
        return ret;
    }

    public static long getLong(HttpServletRequest request, String param) {
        return Long.parseLong(getString(request, param));
    }

    public static long getLong(HttpServletRequest request, String param, long defaultVal) {
        try {
            return getLong(request, param);
        } catch (Exception ex) {
            return defaultVal;
        }
    }

    public static double getDouble(HttpServletRequest request, String param) {
        return Double.parseDouble(getString(request, param));
    }

    public static double getDouble(HttpServletRequest request, String param, double defaultVal) {
        try {
            return getDouble(request, param);
        } catch (Exception ex) {
            return defaultVal;
        }
    }

    public static double[] getDoubles(HttpServletRequest request, String param, double defaultVal) {
        String[] values = getStrings(request, param);
        if (values == null)
            return null;

        double[] ret = new double[values.length];
        for (int i = 0; i < values.length; i++) {
            try {
                ret[i] = Double.parseDouble(values[i]);
            } catch (Exception ex) {
                ret[i] = defaultVal;
            }
        }
        return ret;
    }

    public static Date getDate(HttpServletRequest request, String param) {
        try {
            return DateUtil.parseDate(getString(request, param));
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    public static Date getDateTime(HttpServletRequest request, String param) {
        try {
            return DateUtil.parseDateTime(getString(request, param));
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    public static Date getDate(HttpServletRequest request, String param, Date defaultVal) {
        try {
            Date date = getDate(request, param);
            if (date == null) {
                date = defaultVal;
            }
            return date;
        } catch (Exception ex) {
            return defaultVal;
        }
    }

    public static Date[] getDates(HttpServletRequest request, String param) {
        String[] values = getStrings(request, param);
        if (values == null)
            return null;

        Date[] ret = new Date[values.length];
        for (int i = 0; i < values.length; i++) {
            try {
                ret[i] = DateUtil.parseDate(values[i]);
            } catch (Exception ex) {
                ret[i] = null;
            }
        }
        return ret;
    }

    public static void setSessionAttr(HttpServletRequest request,
                                      String paramName, Object obj) {
        request.getSession().setAttribute(paramName, obj);
    }

    public static Object getSessionAttr(HttpServletRequest request, String paramName) {
        return request.getSession().getAttribute(paramName);
    }

    public static String getSessionString(HttpServletRequest request, String paramName) {
        if (request == null)
            return null;

        return (String) request.getSession().getAttribute(paramName);
    }

    public static void setRequestAttr(HttpServletRequest request,
                                      String paramName, Object obj) {
        request.setAttribute(paramName, obj);
    }

    public static Object getRequestAttr(HttpServletRequest request, String paramName) {
        return request.getAttribute(paramName);
    }
//
//    public static String getSearchParam(SearchParam param, String columnName) {
//      if (param == null)
//          return "";
//
//      String val = null;
//      List<SearchFilter> filters = param.getFilters();
//      if (filters != null) {
//          for (SearchFilter filter : filters) {
//              if (filter.getColumnName().equalsIgnoreCase(columnName)) {
//                  val = filter.getValue();
//                  break;
//              }
//          }
//      }
//        return val == null ? "" : val;
//    }

    private ParamUtil() {
    }
}
